package com.example.drawknightanimation;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;

public class MainActivity extends Activity {

	//define the drawing sub-class object
	DrawView drawView;
	
	//onCreate is called any times the program resumes
	@Override public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		//create the drawing object
		drawView = new DrawView(this);
		setContentView(drawView);
	}
	
	//handle resume/focus events
	@Override public void onResume(){
		super.onResume();
		//pass the resume event on the sub-class
		drawView.resume();
	}
	
	//handle pause/minimize events
	@Override public void onPause(){
		super.onPause();
		//pass the pause event on the sub-class
		drawView.pause();
	}
	
	public class DrawView extends SurfaceView implements Runnable{
		//define the game loop thread
		Thread gameloop = null;
		
		//define the surface holder
		SurfaceHolder surface;
		
		//define the running variable 
		volatile boolean running = false;
		
		//the asset manager handles resource loading
		AssetManager assets = null;
		BitmapFactory.Options options = null;
		Bitmap knight[];
		int frame = 0;
		
		//this is the DrawView class constructor
		public DrawView(Context context){
			super(context);
			
			//get the SurfaceHolder object to supply a context
			surface = getHolder();
			
			//create the asset manager object
			assets = context.getAssets();
			
			//set bitmap color depth option
			options = new BitmapFactory.Options();
			options.inPreferredConfig = Bitmap.Config.ARGB_8888;
			
			//create the bitmap array for animation
			knight = new Bitmap[100];
			
			//load the kinght bitmaps
			try{
				for (int n=0; n<knight.length; n++){
					String filename = "knight"+Integer.toString(n+1)+".png";
					InputStream istream = assets.open(filename);
					knight[n] = BitmapFactory.decodeStream(istream, null, options);
					istream.close();
				}
			}catch(IOException e){
				e.printStackTrace();
			}
			
		}
		
		//custom resume method called by outer class
		public void resume(){
			running = true;
			gameloop = new Thread(this);
			gameloop.start();
		}
		
		//custom pause method called by outer class
		public void pause(){
			running = false;
			while (true){
				try{
					//just keep doing this until app has focus
					gameloop.join();
				}catch (InterruptedException e){}
			}
		}
		
		//this is the threaded method
		@Override public void run(){
			
			//this is the game loop
			while (running){
				
				//make sure surface is usable (it's asynchronous)
				if (!surface.getSurface().isValid())
					continue;
				
				//request the drawing canvas
				Canvas canvas = surface.lockCanvas();
				
				//draw one frame of animation from the knight array
				canvas.drawColor(Color.rgb(85, 107, 47));
				canvas.drawBitmap(knight[frame], 0, 0, null);
				
				//draw the knight scaled larger
				Rect dest = new Rect(0, 128, 200, 400);
				canvas.drawBitmap(knight[frame], null, dest, null);
				
				//draw the knight scaled REALLY BIG
				dest = new Rect(0, 400, 600, 600);
				canvas.drawBitmap(knight[frame], null, dest, null);
				
				//release the canvas
				surface.unlockCanvasAndPost(canvas);
				
				//go to the next frame of animation
				frame++;
				if (frame > knight.length-1) frame = 0;
				
				try{
					//slow down the animation
					Thread.sleep(30);
				}catch(InterruptedException e){
					e.printStackTrace();
				}
			}
		}
	}
}
